package bs_services

import (
	"fmt"
	"log"
	"xiehui-service/app/models/bs_models"
	"xiehui-service/global"

	"go.uber.org/zap"
)

type vehicleService struct {
}

var VehicleService = new(vehicleService)

// 根据ID查询车辆信息
func (vehicleService *vehicleService) GetByID(vehicle_id uint) (vehicle *bs_models.Vehicle, err error) {
	err = global.App.MSACCESS.Ping()
	if err != nil {
		log.Println("ping==>>", err)
		global.App.Log.Error("查询车辆数据失败：", zap.Any("数据库联接", "ping失败"))
		return nil, err
	}
	rows, err := global.App.MSACCESS.Query(fmt.Sprintf("select * from Vehicle where ID = '%d' order by Stamp asc", vehicle_id))
	if err != nil {
		log.Println("Query==>>", err)
		global.App.Log.Error("查询车辆数据失败：", zap.Any("Query查询失败", err))
		return nil, err
	}
	for rows.Next() {
		var v bs_models.Vehicle
		if err := rows.Scan(&v.ID, &v.Code, &v.DriverA, &v.DriverB, &v.DriverC, &v.TranAmnt, &v.Flag, &v.Notes, &v.Stamp); err != nil {
			log.Println("Scan==>>", err)
			global.App.Log.Error("查询车辆数据失败：", zap.Any("Scan查询失败", err))
			return nil, err
		}
		vehicle = &v
	}
	defer rows.Close()

	return vehicle, nil
}
